package com.zjh.photoshopstudy.app.video;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.zjh.photoshopstudy.Model.CommentEntity;
import com.zjh.photoshopstudy.Model.VideoEntity;
import com.zjh.photoshopstudy.R;

import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class VideoCommentFragment extends Fragment {
    private VideoEntity mVideoEntity;
    private TextView tip;
    private OnCommentClickListener commentClickListener;
    private CommentAdapter adapter;
    private RecyclerView recyclerView;

    public VideoCommentFragment() {
        // Required empty public constructor
    }

    public void setCommentClickListener(OnCommentClickListener commentClickListener) {
        this.commentClickListener = commentClickListener;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_video_comment, container, false);
        mVideoEntity = (VideoEntity) getArguments().getSerializable("videoEntity");
        recyclerView = view.findViewById(R.id.comment_list);
        tip = view.findViewById(R.id.no_comment_tip);
        adapter = new CommentAdapter(null, mVideoEntity, getContext(), commentClickListener);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        VideoHttp.getVideoComment(this, mVideoEntity.getId());
        return view;
    }

    public void onGetVideoCommentSuccess(List<CommentEntity> list) {
        adapter.setvCommentEntityList(list);
        adapter.notifyDataSetChanged();
        if (list.size() > 0) {
            tip.setVisibility(View.INVISIBLE);
        }
    }

    public void onSendCommentSuccess(CommentEntity commentEntity) {
        List<CommentEntity> list = adapter.getvCommentEntityList();
        list.add(0, commentEntity);
        adapter.notifyDataSetChanged();
        if (list.size() > 0) {
            tip.setVisibility(View.INVISIBLE);
        }
    }

    public void onGetVideoCommentFail() {
        Toast.makeText(getContext(), "获取评论失败", Toast.LENGTH_SHORT).show();
    }
}
